from django.shortcuts import render
from .forms import UserForm
from .models import User,member
from django.http import HttpResponse
import os
# Create your views here.


# def add_user(request):
#    if request.method=='POST':
#        user_reg=UserForm(request.POST)
#        if user_reg.is_valid():
#            User.objects.create(
#                username=user_reg.cleaned_data['name'],
#                password=user_reg.cleaned_data['password'],
#                email=user_reg.cleaned_data['email'],
#            )
#        return HttpResponse(user_reg)
#    else:
#        user_reg=UserForm()
#    return render(request,'index.html',locals())


#注册的页面
def add_user(request):
   if request.method=='POST':
       name=request.POST['User Name']
       email=request.POST['Email']
       Password=request.POST['Password']


       User.objects.create(
           username=name,
           password=Password,
           email=email,
       )
       return render(request,'sucess.html',locals())

   return render(request,'index.html')

def denglu(request):
   if request.method=='POST':

       name=request.POST['User Name']#得到用户输入的名字
       Password=request.POST['Password']#得到用户输入的密码
       try:
           user_name=User.objects.get(username=name)
           Real_password=User.objects.get(username=user_name).password#查询到用户的密码
           if(Password==Real_password):
               return render(request,'upload.html')
           else:
               return render(request, 'index.html')
       except:

             return render(request, 'index.html')

   return render(request,'index.html')

def upload_pic(request):
    if(request.method=='POST'):
        name=request.POST['User Name']
        email=request.POST['Email']
        pic=request.FILES.get('photo')
        dest='E:\\photo\\'
        pic_url=dest+pic.name
        f = open(os.path.join(dest, pic.name), 'wb')
        for line in pic.chunks():
            f.write(line)
        f.close()
        member.objects.create(
            name=name,
            email=email,
            pic_url=pic_url
        )
        return render(request,'upload_scess.html',locals())